package com.woniuxy.es;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.stream.Collectors;

@SpringBootApplication
@Controller
public class EsMain {
    @Autowired
    ESBookRepository repository;
    @Autowired
    BookMapper bookMapper;
    @Autowired
    BooktypeMapper booktypeMapper;

    public static void main(String[] args) {
        SpringApplication.run(EsMain.class);
    }

    @RequestMapping("initEs")
    @ResponseBody
    public  List<EsBook>  initEsBookData() {
//将mysql中的数据全部数据读取出来，然后写入es中
        List<Book> books = bookMapper.selectList(null);
        List<EsBook> esBookList = books.stream().map(book -> {
            EsBook esbook = new EsBook();
            EsBook esBook = new EsBook();
            esBook.setId(book.getId());
            esBook.setName(book.getName());
            esBook.setTypeId(book.getTypeId());
            // 补充bookName
            Booktype booktype = booktypeMapper.selectById(book.getTypeId());
            esBook.setTypeName(booktype.getName());
            esBook.setProvider(book.getProvider());
            esBook.setAuthor(book.getAuthor());
            esBook.setPrice(book.getPrice());
            esBook.setDetail(book.getDetail());
            esBook.setImgsrc(book.getImgsrc());
            esBook.setCollectioncount(book.getCollectioncount());
            esBook.setStorecount(book.getStorecount());
            esBook.setBuycount(book.getBuycount());
            esBook.setReadcount(book.getReadcount());
            esBook.setCreatetime(book.getCreatetime());
            esBook.setUpdatetime(book.getUpdatetime());
            esBook.setState(book.getState());
            return esBook;
        }).collect(Collectors.toList());
        repository.saveAll(esBookList);
        esBookList.forEach(System.out::println);
        return esBookList;
    }
    @RequestMapping("selectESBook")
    @ResponseBody
    public Page<EsBook> selectESBook(Integer currentPage, Integer pageSize, String keyword){
        //构件查询条件对象
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        //query条件
        nativeSearchQueryBuilder.withQuery(QueryBuilders.multiMatchQuery(keyword, "name", "typeName", "provider", "author", "detail"));
        //分页条件   es里面分页页码从0开始
        nativeSearchQueryBuilder.withPageable(PageRequest.of(currentPage-1, pageSize));
        //排序条件
        nativeSearchQueryBuilder.withSort(SortBuilders.fieldSort("buycount").order(SortOrder.DESC));
        //查询
        org.springframework.data.domain.Page<EsBook> search = repository.search(nativeSearchQueryBuilder.build());
        Page<EsBook> page = new Page<>(currentPage,pageSize);
        //将es的page 替换成mybatisplus 的page对象
        page.setRecords(search.getContent());
        page.setCurrent(search.getNumber());
        page.setTotal(search.getTotalElements());
        page.setPages(search.getTotalPages());
        return page;
    }
}
